
package net.swa.index.web.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import net.swa.dewei.beans.entity.DingDan;
import net.swa.system.web.action.AbstractBaseAction;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

/**
 * 前台界面抽象action 基类
 * @author dawei chen
 *
 */
public abstract class AbstractBaseIndexAction extends AbstractBaseAction
{

    private static final long serialVersionUID = 3625388438776145908L;

    private final Logger log = Logger.getLogger(AbstractBaseIndexAction.class);

    /**
     * 从session中取订单实体，取不到查cookie
     * @return
     */
    protected DingDan getModelSessionCookie()
    {
        DingDan m = null;
        m = (DingDan) getSessionAttribute("dingDanModel");
        if (null == m)
        {
            m = new DingDan();
            HttpServletRequest request = ServletActionContext.getRequest();
            Cookie[] cookies = request.getCookies();
            if (null != cookies && cookies.length > 0)
            {
                for (Cookie cookie : cookies)
                {
                    try
                    {
                        String value = URLDecoder.decode(cookie.getValue(), "UTF-8");
                        if (cookie.getName().equals("id"))
                        {
                            m.setId(Long.parseLong(value));
                        }
                        else if (cookie.getName().equals("dingDanUid"))
                        {
                            m.setDingDanUid(value);
                        }
                        else if (cookie.getName().equals("userId"))
                        {
                            m.setUserId(value);
                        }
                        else if (cookie.getName().equals("chexingId"))
                        {
                            m.setChexingId(Long.parseLong(value));
                        }
                        else if (cookie.getName().equals("chexingName"))
                        {
                            m.setChexingName(value);
                        }
                        else if (cookie.getName().equals("mendianId"))
                        {
                            m.setMendianId(Long.parseLong(value));
                        }
                        else if (cookie.getName().equals("mendianLxr"))
                        {
                            m.setMendianLxr(value);
                        }
                        else if (cookie.getName().equals("mendianName"))
                        {
                            m.setMendianName(value);
                        }
                        else if (cookie.getName().equals("lat"))
                        {
                            m.setLat(Double.parseDouble(value));
                        }
                        else if (cookie.getName().equals("lng"))
                        {
                            m.setLng(Double.parseDouble(value));
                        }
                        else if (cookie.getName().equals("blat"))
                        {
                            m.setBlat(Double.parseDouble(value));
                        }
                        else if (cookie.getName().equals("blng"))
                        {
                            m.setBlng(Double.parseDouble(value));
                        }
                        else if (cookie.getName().equals("mlat"))
                        {
                            m.setMlat(Double.parseDouble(value));
                        }
                        else if (cookie.getName().equals("mlng"))
                        {
                            m.setMlng(Double.parseDouble(value));
                        }
                        else if (cookie.getName().equals("dianchiId"))
                        {
                            m.setDianchiId(Long.parseLong(value));
                        }
                        else if (cookie.getName().equals("imageUrl"))
                        {
                            m.setImageUrl(value);
                        }

                        else if (cookie.getName().equals("pinPai"))
                        {
                            m.setPinPai(value);
                        }
                        else if (cookie.getName().equals("xingHao"))
                        {
                            m.setXingHao(value);
                        }
                        else if (cookie.getName().equals("price"))
                        {
                            m.setPrice(value);
                        }

                        else if (cookie.getName().equals("kehuName"))
                        {
                            m.setKehuName(value);
                        }
                        else if (cookie.getName().equals("lxTel"))
                        {
                            m.setLxTel(value);
                        }
                        else if (cookie.getName().equals("menDianTel"))
                        {
                            m.setMenDianTel(value);
                        }
                        else if (cookie.getName().equals("changeType"))
                        {
                            m.setChangeType(value);
                        }

                        else if (cookie.getName().equals("address"))
                        {
                            m.setAddress(value);
                        }

                        else if (cookie.getName().equals("mendianAddr"))
                        {
                            m.setMendianAddr(value);
                        }
                        else if (cookie.getName().equals("time"))
                        {
                            m.setTime(value);
                        }

                        else if (cookie.getName().equals("crateDate"))
                        {
                            m.setCrateDate(value);
                        }
                        else if (cookie.getName().equals("updateDate"))
                        {
                            m.setUpdateDate(value);
                        }
                        else if (cookie.getName().equals("state"))
                        {
                            m.setState(value);
                        }
                        else if (cookie.getName().equals("province"))
                        {
                            m.setProvince(value);
                        }
                        else if (cookie.getName().equals("city"))
                        {
                            m.setCity(value);
                        }
                        else if (cookie.getName().equals("town"))
                        {
                            m.setTown(value);
                        }

                        else if (cookie.getName().equals("zhongliang"))
                        {
                            m.setZhongliang(value);
                        }

                        else if (cookie.getName().equals("licheng"))
                        {
                            m.setLicheng(value);
                        }
                        else if (cookie.getName().equals("marketPrice"))
                        {
                            m.setMarketPrice(value);
                        }
                        else if (cookie.getName().equals("serverPrice"))
                        {
                            m.setServerPrice(value);
                        }
                    }
                    catch (UnsupportedEncodingException e)
                    {
                        log.debug("UnsupportedEncodingException 转化中文");
                        e.printStackTrace();
                    }
                }
            }
        }
        return m;
    }

    /**
     * 向session、cookie中添加实体
     * @param model
     */
    protected void addModelSessionCookie(DingDan model)
    {
        if (model != null)
        {
            setSessionAttribute("dingDanModel", model);
            //            if (null != model.getId())
            //            {
            //                addCookie("id", model.getId().toString());
            //            }
            if (!StringUtils.isBlank(model.getDingDanUid()))
            {
                addCookie("dingDanUid", model.getDingDanUid());
            }
            if (!StringUtils.isBlank(model.getUserId()))
            {
                addCookie("userId", model.getUserId());
            }
            if (null != model.getChexingId())
            {
                addCookie("chexingId", model.getChexingId().toString());
            }
            if (!StringUtils.isBlank(model.getChexingName()))
            {
                addCookie("chexingName", model.getChexingName());
            }
            if (null != model.getMendianId())
            {
                addCookie("mendianId", model.getMendianId().toString());
            }
            if (!StringUtils.isBlank(model.getMendianLxr()))
            {
                addCookie("mendianLxr", model.getMendianLxr());
            }
            if (!StringUtils.isBlank(model.getMendianName()))
            {
                addCookie("mendianName", model.getMendianName());
            }
            if (null != model.getLat())
            {
                addCookie("lat", model.getLat().toString());
            }
            if (null != model.getLng())
            {
                addCookie("lng", model.getLng().toString());
            }
            if (null != model.getBlat())
            {
                addCookie("blat", model.getBlat().toString());
            }
            if (null != model.getBlng())
            {
                addCookie("blng", model.getBlng().toString());
            }
            if (null != model.getMlat())
            {
                addCookie("mlat", model.getMlat().toString());
            }
            if (null != model.getMlng())
            {
                addCookie("mlng", model.getMlng().toString());
            }
            if (null != model.getDianchiId())
            {
                addCookie("dianchiId", model.getDianchiId().toString());
            }
            if (!StringUtils.isBlank(model.getImageUrl()))
            {
                addCookie("imageUrl", model.getImageUrl().toString());
            }
            if (!StringUtils.isBlank(model.getPinPai()))
            {
                addCookie("pinPai", model.getPinPai().toString());
            }
            if (!StringUtils.isBlank(model.getXingHao()))
            {
                addCookie("xingHao", model.getXingHao().toString());
            }
            if (!StringUtils.isBlank(model.getPrice()))
            {
                addCookie("price", model.getPrice());
            }
            if (!StringUtils.isBlank(model.getKehuName()))
            {
                addCookie("kehuName", model.getKehuName().toString());
            }
            if (!StringUtils.isBlank(model.getLxTel()))
            {
                addCookie("lxTel", model.getLxTel().toString());
            }

            if (!StringUtils.isBlank(model.getMenDianTel()))
            {
                addCookie("menDianTel", model.getMenDianTel().toString());
            }
            if (!StringUtils.isBlank(model.getChangeType()))
            {
                addCookie("changeType", model.getChangeType().toString());
            }
            if (!StringUtils.isBlank(model.getAddress()))
            {
                addCookie("address", model.getAddress().toString());

            }
            if (!StringUtils.isBlank(model.getMendianAddr()))
            {
                addCookie("mendianAddr", model.getMendianAddr().toString());
            }
            if (!StringUtils.isBlank(model.getTime()))
            {
                addCookie("time", model.getTime().toString());
            }
            if (!StringUtils.isBlank(model.getCrateDate()))
            {
                addCookie("crateDate", model.getCrateDate().toString());
            }
            if (!StringUtils.isBlank(model.getUpdateDate()))
            {
                addCookie("updateDate", model.getUpdateDate().toString());
            }
            if (!StringUtils.isBlank(model.getState()))
            {
                addCookie("state", model.getState().toString());
            }

            if (!StringUtils.isBlank(model.getProvince()))
            {
                addCookie("province", model.getProvince().toString());
            }
            if (!StringUtils.isBlank(model.getCity()))
            {
                addCookie("city", model.getCity().toString());
            }
            if (!StringUtils.isBlank(model.getTown()))
            {
                addCookie("town", model.getTown().toString());
            }
            if (!StringUtils.isBlank(model.getZhongliang()))
            {
                addCookie("zhongliang", model.getZhongliang().toString());
            }

            if (!StringUtils.isBlank(model.getLicheng()))
            {
                addCookie("licheng", model.getLicheng().toString());
            }

            if (!StringUtils.isBlank(model.getDetailInfo()))
            {
                addCookie("detailInfo", model.getDetailInfo().toString());
            }

            if (!StringUtils.isBlank(model.getMarketPrice()))
            {
                addCookie("marketPrice", model.getMarketPrice().toString());
            }

            if (!StringUtils.isBlank(model.getServerPrice()))
            {
                addCookie("serverPrice", model.getServerPrice().toString());
            }

        }
    }

    protected void addCookie(String name , String value)
    {
        try
        {
            value = URLEncoder.encode(value, "UTF-8");
            Cookie cookie = new Cookie(name, value);
            cookie.setMaxAge(60 * 60 * 24 * 365);
            ServletActionContext.getResponse().addCookie(cookie);
        }
        catch (UnsupportedEncodingException e)
        {
            log.debug("UnsupportedEncodingException 转化中文");
            e.printStackTrace();
        }
    }

    /**
     * Cookie取得
     * @return
     * @throws Exception
     */
    protected String getCookie(String name)
    {
        HttpServletRequest request = ServletActionContext.getRequest();
        Cookie[] cookies = request.getCookies();
        if (null != cookies && cookies.length > 0)
        {
            for (Cookie cookie : cookies)
            {
                if (cookie.getName().equals(name))
                {
                    return cookie.getValue();
                }
            }
        }
        return null;
    }
}
